// import {getToken, WsPATH} from "../../common/request";
import api from '@/config/api'
import storage from '@/utils/storage'

let beatInterval: any

//启动
export function start() {
  setTimeout(() => {
    heartbeat()
  }, 200)
  if (beatInterval) {
    stop()
  }
  beatInterval = setInterval(heartbeat, 10000);
}

//停止
export function stop() {
  clearInterval(beatInterval);
}

//心跳
export function heartbeat() {
  uni.sendSocketMessage({
    data: '',
    /*success() {
      console.log('----心跳成功----');
    },*/
    fail() {
      console.log('心跳错误');
      // 重试登录
      var socketTask = uni.connectSocket({
        url: api.imWs + "?token=" + storage.getAccessToken(),
        complete: ()=> {}
      });
      console.log('重连中', socketTask)
    }
  });
}

//发送消息
export const sendChatMessage = (msg: any, call?: Function) => {
  
  let msgData = {
    operationType: "MESSAGE",
    to: msg.userId,
    from: msg.fromId,
    messageType: "MESSAGE",
    context: msg.message,
    talkId: msg.talkId,
  }
  // console.log(msg)
  uni.sendSocketMessage({
    data: JSON.stringify(msgData),
    success() {
      call && call()
    }, fail() {
      // 重试登录
      // getToken().then(token => {
      //   uni.connectSocket({
      //     url: WsPATH + token,
      //     method: 'GET'
      //   });
      // })
      uni.showToast({
        title: '消息发送失败，请稍后再试！',
        icon: 'error',
        duration: 1000
      });
    }
  });
}

// const innerAudioContext = uni.createInnerAudioContext();
// innerAudioContext.autoplay = false;
// innerAudioContext.startTime = 0.8;
// uni.downloadFile({
//   url: 'https://im.idb.com.cn/fs/static/msgTip.mp3',
//   success: (res) => {
//     if (res.tempFilePath) {
//       innerAudioContext.src = res.tempFilePath;
//     }
//   },
//   fail: (err) => {
//     console.log("err", err);
//   },
// });
// innerAudioContext.onPlay(() => {
//   // console.log('开始播放');
//   uni.vibrateLong({})
// });

// export const msgTip = () => {
//   innerAudioContext.play();
// }